﻿package com.xiaoqee.hadoop.study.hdfs;

import java.io.InputStream;
import java.net.URL;

import org.apache.hadoop.fs.FsUrlStreamHandlerFactory;
import org.apache.hadoop.io.IOUtils;

public class HdfsURLMain {
	
	/**
	 *  注册，让Java程序识别HDFS URL形式
	 *  This method can be called at most once in a given Java Virtual Machine. 
	 */
	static {
		URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
	}
	public static void main(String[] args) {
		String fileUrl = "hdfs://hadoop-zengjr.xiaoqee.com:8020/user/zengjr/mr/wordcount/input/wc.input";
		readByUrl(fileUrl);
	}
	
	public static void readByUrl(String fileUrl) {

		// 打开输入流
		InputStream inStream = null;
		try {
			inStream = new URL(fileUrl).openStream();
			IOUtils.copyBytes(inStream, System.out, 4096, false);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			IOUtils.closeStream(inStream);
		}
	}

}
